//
// Created by csy on 2023/5/30.
//

#ifndef TINYTINY_RPC_TIMER_H
#define TINYTINY_RPC_TIMER_H

#include <set>
#include <memory>
#include "../common/mutex.h"
#include "fdevent.h"
#include "timer_event.h"

namespace rocket {

  class Timer : public FdEvent {

  public:
    typedef std::shared_ptr<Timer> ptr;
    Timer();
    ~Timer() = default;

    void addTimerEvent(const TimerEvent::ptr& event);
    void deleteTimerEvent(const TimerEvent::ptr& event);
    void onTimer(); // 当发送了 IO 事件后，eventloop 会执行这个回调函数 取出所有的超时任务

  private:
    void resetArriveTime();
  private:
    std::set<TimerEvent::ptr, TimerEvent::Compare> _pending_events;
    Mutex _mutex;
  };
}

#endif //TINYTINY_RPC_TIMER_H